package org.com.ashen.service.service.datasourcemanage;

import org.com.ashen.client.service.IDataSourceService;
import org.com.ashen.common.common.HttpResult;
import org.com.ashen.common.common.datasource.DataSourceDTO;
import org.com.ashen.dal.dataobject.DataSourceDO;
import org.com.ashen.service.dao.DataSourceDao;
import org.com.ashen.service.datasourceswitch.DataSourceBeanBuilder;
import org.com.ashen.service.datasourceswitch.DataSourceContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("dataSourceService")
public class DataSourceServiceImpl implements IDataSourceService {
	@Resource(name = "datasourceDao")
	private DataSourceDao dataSourceDao;

	@Override
	public HttpResult<DataSourceDTO> getDataSource(String datasourcename) {
		List<DataSourceDO> dataSourceDOList = this.dataSourceDao
				.queryDataSource(datasourcename);
		DataSourceDO dataSourceDO = dataSourceDOList.get(0);

		DataSourceBeanBuilder builder = new DataSourceBeanBuilder(
				dataSourceDO.getDatabaseName(), 
				dataSourceDO.getDatabaseIp(),
				dataSourceDO.getDatabasePort(),
				dataSourceDO.getDatasourceName(), 
				dataSourceDO.getUsername(),
				dataSourceDO.getPassword());
		DataSourceContext.setDataSource(builder);
		DataSourceContext.clearDataSource();

		DataSourceDTO dataSourceDTO = DatasourceConvert
				.conventToDatasourceDTO(dataSourceDO);
		return HttpResult.successResult(dataSourceDTO);
	}

}
